import Vue from 'vue'
import Vuex from 'vuex'
import http from '../utils/request'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 用户登录状态
    userLogin:sessionStorage.getItem('userLogin')?JSON.parse(sessionStorage.getItem('userLogin')):false,
    // 用户订单详情
    userDin:sessionStorage.getItem('userDin')&&sessionStorage.getItem('userDin') !== undefined?JSON.parse(sessionStorage.getItem('userDin')):[],
    // 所有商家
    allShop:sessionStorage.getItem('shop')?JSON.parse(sessionStorage.getItem('shop')):[
      {
        "_id": "",
        // 场地编号
        "placeid": "001",
        // 租聘状态
        "isFull":false,
        // 租聘人
        "s_name": "",
        // 商家类型
        "s_type": "",
        // 商家简介
        "s_content": "",
        // 商家图片
        "s_Images": [],
        // 场地价格 勿改
        "p_price": 0,
        // 活动价格
        "s_price": 0,
        // 无用的,但别修改
        "s_id": ""
    }
    ],
    // 用户信息
    user:sessionStorage.getItem('user')&&sessionStorage.getItem('user') !== undefined?JSON.parse(sessionStorage.getItem('user')):{username:"用户名",  categary:"用户"}
  },
  getters: {
    // 以被租用的场地信息
    excellentShop(state){
      return state.allShop.filter(item=>{
        if(item.isFull&&item.s_content !=="官方占用"){
          return item
        }
      }) || []
    },
     // 判断商家是否已经预定场地
     userShop(state){
      return state.allShop.some(item=>{
        return item.s_name === state.user.username
      })

     }
  },
  mutations: {
    // 所有商家
    GETALLSHOP(state,res){
      state.allShop = res
      sessionStorage.setItem("shop",JSON.stringify(res))
    },
    //修改订单
    GEtALLUSERDIN(state,obj){
      let obj2
      let index 
      for(let i= 0;i<state.userDin.length;i++){
        if(state.userDin[i].s_name === obj.s_name){
          index = i
          let num = +state.userDin[i].s_num+obj.s_num
          obj2 = {...state.userDin[i],s_num:num}
        }
      }
      if(obj2 !== undefined){
        let arr2 = [...state.userDin]
        arr2[index] = obj2
        state.userDin = arr2
      }else{
        state.userDin = [...state.userDin,obj]
      }
      sessionStorage.setItem('userDin',JSON.stringify(state.userDin))
    },
    // 修改个人信息和登录状态
    GETUSER(state,res){
      state.user = {
        username: res.username,
        categary: res.categary,
        picUrl: res.picUrl
      }
      state.userLogin = !state.userLogin
      sessionStorage.setItem("user",JSON.stringify(res))
      sessionStorage.setItem("userLogin",JSON.stringify(state.userLogin))
      if(res.categary !== '商家'){
      state.userDin = res.userOrder
      sessionStorage.setItem("userDin",JSON.stringify(res.userOrder))
      }
    },
    // 修改用户头像
    CHANGEPIC(state,userPic) {
      let obj = {...state.user}
      obj.picUrl[0] = userPic
      state.user = obj
      sessionStorage.setItem("user",JSON.stringify(state.user))
    },
    // 退出登录
    EXITLOGIN(state){
      state.user= {username:"用户名",  categary:"用户"}
      state.userLogin = false
      state.userDin = []
      sessionStorage.removeItem("userLogin")
      sessionStorage.removeItem("userDin")
      sessionStorage.removeItem("user")
      sessionStorage.removeItem("currentPage")
    },
    // 取消单个预约订单
    CHANGEDIN(state,s_name){
     let arr = state.userDin.filter(item=>{
        return item.s_name !== s_name
      })
      state.userDin = arr
      sessionStorage.setItem('userDin',JSON.stringify(state.userDin))
    },
  },
  actions: {
    // 获取所有商家
   async getAllShop({commit}){
    let res = await http.get("http://10.3.136.136:8888/getshop")
    commit("GETALLSHOP",res)
    },
  },
  modules: {
 
  }
})
